/* 
 * 
 * Created on 17 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package actions;

import gui.GUIUtils;
import gui.main.MainPanelGUIView;

import java.awt.Color;
import java.net.URI;


import objects.MessageCodes;
import objects.data.Message;
import objects.data.RegisterData;
import utils.Utils;
import web.Hosts;
import web.WebSocket;

/**
 *
 * @author simonk
 */
public class Register {
	public static void doRegister(RegisterData ri)
	{
		WebSocket ws = Hosts.ws;
		MainPanelGUIView mainPanel = GUIUtils.MainPanel;
		try {
			if(ws == null)
			{
				ws = new WebSocket(new URI(Hosts.SERVER));
				ws.connect();
			}
			ws.send(ri);
			Message m = Message.fromJsonSplit(ws.recv());
			if(m.type == MessageCodes.OK) {
				mainPanel.setStatusLbl("Registration successful!", Color.GREEN);
			} else {
				mainPanel.setStatusLbl("Registration failed: "+m.msg, Color.RED);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mainPanel.setStatusLbl("Registration failed: "+e.getMessage(), Color.RED);
		}
	}
}
